from sqlmodel import SQLModel,Date,Numeric,Relationship,Field
from typing import Annotated
from datetime import date


class SecurityBase(SQLModel):
    symbol: Annotated[str,Field(max_length=20,description="股票代码",index=True,unique=True)]
    name:  Annotated[str,Field(index=True,unique=True)]
    exchange: Annotated[str,Field()]

class Security(SecurityBase,table=True):
    id:Annotated[int|None,Field(default=None,primary_key=True)]
    # historical_data: Annotated[list["HistoricalData"],Relationship(back_populates="security")]
    historical_data: list["HistoricalData"]=Relationship(back_populates="security")


class HistoricalDataBase(SecurityBase):
    date: Annotated[date,Field(sa_type=Date)]
    open: Annotated[float,Field(sa_type=Numeric)]
    high: Annotated[float,Field(sa_type=Numeric)]
    low: Annotated[float,Field(sa_type=Numeric)]
    close: Annotated[float,Field(sa_type=Numeric)]
    volume: Annotated[float,Field(sa_type=Numeric)]
    turnover: Annotated[float|None,Field(sa_type=Numeric)]

class HistoricalData(HistoricalDataBase,table=True):
    id : Annotated[int|None,Field(primary_key=True)]
    security_id: Annotated[int,Field(foreign_key="security.id")]
    # security: Annotated[Security,Relationship(back_populates="historical_data")]
    security:Security=Relationship(back_populates="historical_data")
